﻿using Dou.UI;
using UnityEngine;

namespace Simple
{
    public class Test_DragCard : MonoBehaviour
    {
        public Draggable draggable;

        public string data;

        public GameObject mask;

        public GameObject dragItem;
        
        private void Start()
        {
            draggable.onDragBegin.AddListener((nowPos, originPos, camera) =>
            {
                mask.SetActive(true);
                dragItem.SetActive(true);

                // var worldPos = transform.parent.TransformPoint(nowPos);
                // var screenPoint = RectTransformUtility.WorldToScreenPoint(camera, worldPos);
                // if (RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform) dragItem.transform.parent, screenPoint, camera, out var localPoint))
                // {
                //     dragItem.transform.localPosition = localPoint;
                // }

                if (UIUtil.GetLocalPointInOtherRectangle((RectTransform) transform.parent, nowPos, (RectTransform) dragItem.transform.parent, camera, out var localPoint))
                {
                    dragItem.transform.localPosition = localPoint;
                }
            });
            draggable.onDragging.AddListener((nowPos, originPos, camera) =>
            {
                // var worldPos = transform.parent.TransformPoint(nowPos);
                // var screenPoint = RectTransformUtility.WorldToScreenPoint(camera, worldPos);
                // if (RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform) dragItem.transform.parent, screenPoint, camera, out var localPoint))
                // {
                //     dragItem.transform.localPosition = localPoint;
                // }
                
                if (UIUtil.GetLocalPointInOtherRectangle((RectTransform) transform.parent, nowPos, (RectTransform) dragItem.transform.parent, camera, out var localPoint))
                {
                    dragItem.transform.localPosition = localPoint;
                }
            });
            draggable.onDragEnd.AddListener((nowPos, originPos, camera) =>
            {
                mask.SetActive(false);
                dragItem.SetActive(false);
            });
        }
    }
}
